/* -------------------------------------------------------------------------- *
   TestWinUnit - Maria Blees (maria.blees@microsoft.com)

   File: MasterLoggerTest.cpp
   Contains tests for the MasterLogger class.
 * -------------------------------------------------------------------------- */
#include "WinUnit.h"

#include "MasterLogger.h"
#include "MockLogger.h"

using namespace WinUnitLib;

// This test doesn't assert anything but just makes sure there's no crash
// or unexpected behavior if there are no loggers in the chain.
BEGIN_TEST(MasterLoggerHandlesNoLoggersInChain)
{
    MasterLogger masterLogger(false, false, 0);
    
    masterLogger.OutputTestExecutableNamePreTest(
        L"foo", L"bar");

    masterLogger.OutputTestExecutableNamePostTest(
        L"foo", L"bar", 0, 0);

    masterLogger.OutputTestNamePreTest("test1");
    masterLogger.OutputTestNamePostTest("test1", true);

    masterLogger.OutputTrace(L"trace1");
    masterLogger.OutputTestError(L"error1");

    masterLogger.OutputProgramErrorLine(L"hello", L"world");

    masterLogger.OutputFinalResults(0, 0);
}
END_TEST


BEGIN_TEST(MasterLoggerHandlesCustomLogger)
{
    MasterLogger masterLogger(false, false, 0);
    MockLogger customLogger;

    masterLogger.AddLogger(&customLogger);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTestExecutableNamePreTestCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTestExecutableNamePostTestCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTestNamePreTestCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTestNamePostTestCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTraceCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputTestErrorCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputProgramErrorLineCalled);
    WIN_ASSERT_EQUAL(0, customLogger.OutputFinalResultsCalled);
    
    masterLogger.OutputTestExecutableNamePreTest(
        L"foo", L"bar");

    WIN_ASSERT_EQUAL(1, customLogger.OutputTestExecutableNamePreTestCalled);

    masterLogger.OutputTestExecutableNamePostTest(
        L"foo", L"bar", 0, 0);

    WIN_ASSERT_EQUAL(1, customLogger.OutputTestExecutableNamePostTestCalled);

    masterLogger.OutputTestNamePreTest("test1");
    WIN_ASSERT_EQUAL(1, customLogger.OutputTestNamePreTestCalled);
    masterLogger.OutputTestNamePostTest("test1", true);
    WIN_ASSERT_EQUAL(1, customLogger.OutputTestNamePostTestCalled);

    masterLogger.OutputTrace(L"trace1");
    WIN_ASSERT_EQUAL(1, customLogger.OutputTraceCalled);
    masterLogger.OutputTestError(L"error1");
    WIN_ASSERT_EQUAL(1, customLogger.OutputTestErrorCalled);

    masterLogger.OutputProgramErrorLine(L"hello", L"world");
    WIN_ASSERT_EQUAL(1, customLogger.OutputProgramErrorLineCalled);

    masterLogger.OutputFinalResults(0, 0);
    WIN_ASSERT_EQUAL(1, customLogger.OutputFinalResultsCalled);
}
END_TEST

BEGIN_TEST(MasterLoggerSetVerbositySetsVerbosityOnChildren)
{
    MasterLogger masterLogger(false, false, 0);
    
    MockLogger logger1;
    logger1.SetVerbosity(3);
    WIN_ASSERT_EQUAL(3U, logger1.Verbosity);

    MockLogger logger2;
    logger2.SetVerbosity(3);
    WIN_ASSERT_EQUAL(3U, logger2.Verbosity);

    masterLogger.AddLogger(&logger1);
    masterLogger.AddLogger(&logger2);

    // Verify that master logger set the child loggers' verbosities to its own 
    // when they were added.
    WIN_ASSERT_EQUAL(0U, logger1.Verbosity);
    WIN_ASSERT_EQUAL(0U, logger2.Verbosity);

    // Now updating master logger's verbosity also updates children's.
    masterLogger.SetVerbosity(2);
    WIN_ASSERT_EQUAL(2U, logger1.Verbosity);
    WIN_ASSERT_EQUAL(2U, logger2.Verbosity);
}
END_TEST